package com.rem.zookeeper.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Rem
 * @date 2022-12-04
 */
@Configuration
public class CuratorConfig {

    @Autowired
    Wrapper wrapper;

    @Bean(initMethod = "start", destroyMethod = "close")
    public CuratorFramework curatorFramework() {
        /**
         * 创建新客户端
         * connectString——要连接的服务器列表
         * sessionTimeoutMs – 会话超时
         * connectionTimeoutMs – 连接超时
         * retryPolicy – 要使用的重试策略
         */
        return CuratorFrameworkFactory.newClient(
                wrapper.getConnectString(),
                wrapper.getSessionTimeoutMs(),
                wrapper.getConnectionTimeoutMs(),
                new RetryNTimes(wrapper.getRetryCount(), wrapper.getElapsedTimeMs()));
    }
}
